<?php

class myUser extends sfGuardSecurityUser
{
  public function signOut()
  {   
	parent::signOut();	 
	$this->getAttributeHolder()->clear();
  }
  public function signIn($user, $remember = false, $con = null)
  {
    parent::signIn($user, $remember, $con);
	$projects = $this->getGuardUser()->getResource()->getProjectsOfLoggedInUser();
	if ($projects == NULL)
	  return;
	$selectedProject = $projects->get(0);
	if (isset($selectedProject))
	  $this->setAttribute('selectedProject', $selectedProject);	
  }
  public function getUserId()
  {
    if($this->isAuthenticated())
       return $this->getGuardUser()->getId();
    else
       return 0;
  }
  public function getResourceId()
  {
    if($this->isAuthenticated())
       return $this->getGuardUser()->getResource()->getResourceid();
    else
       return 0;
  }
  public function getResourceName()
  {
    if($this->isAuthenticated())
       return $this->getGuardUser()->getResource()->getName();
    else
       return 0;
  }
  public function getProjectsDropdown()
  {
    $projects = $this->getGuardUser()->getResource()->getProjectsOfLoggedInUser();
	
	if ($projects != NULL and count($projects) > 0)
	{
      //$this->setAttribute('selectedProject', $projects[0]);
	
	  $projectsDropdown = NULL;
	  $selectedProject = $this->getAttribute('selectedProject');	  
	  foreach ($projects as $i => $project)	
	  { 
	    echo $project->getProjectname()."<br/>";
		if ($i == 0 && !$selectedProject)
		{
		  $this->setAttribute('selectedProject', $project);
		  $selectedProject = $project;
		}

	    $projectsDropdown .= "<option ";
	    if ($selectedProject->getProjectid() == $project->getProjectid())
	    {
		  $projectsDropdown .= "selected ";
	    }		  
	    $projectsDropdown .= "value='".$project->getProjectid()."'>".$project->getProjectname()."</option>";  				
	  }      
	  return $projectsDropdown;
	}
	else
	{
	  return "<option value='-1'>User Has No Projects</options>";
	}
  }
}
